<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="100px">
          <!--          <el-form-item label="分类id" prop="flId">
                      <el-input v-model="queryParams.flId" placeholder="请输入分类id" clearable style="width: 240px" @keyup.enter="handleQuery" />
                    </el-form-item>-->
          <!--          <el-form-item label="信息类型" prop="type">-->
          <!--            <el-select v-model="queryParams.type" placeholder="请选择信息类型" clearable>-->
          <!--              <el-option-->
          <!--                v-for="dict in business_information_type"-->
          <!--                :key="dict.value"-->
          <!--                :label="dict.label"-->
          <!--                :value="dict.value"-->
          <!--              />-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->
          <el-form-item label="标题" prop="bt">
            <el-input v-model="queryParams.bt" placeholder="请输入标题" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="内容" prop="nr">
            <el-input v-model="queryParams.nr" placeholder="请输入内容" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <!--          <el-form-item label="图片" prop="photo">
                      <el-input v-model="queryParams.photo" placeholder="请输入图片" clearable style="width: 240px" @keyup.enter="handleQuery" />
                    </el-form-item>-->
          <!--          <el-form-item label="地址" prop="dz">-->
          <!--            <el-input v-model="queryParams.dz" placeholder="请输入地址" clearable style="width: 240px" @keyup.enter="handleQuery" />-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="身份" prop="sf">
                      <el-input v-model="queryParams.sf" placeholder="请输入身份" clearable style="width: 240px" @keyup.enter="handleQuery" />
                    </el-form-item>-->
          <el-form-item label="联系人姓名" prop="lxrXm">
            <el-input v-model="queryParams.lxrXm" placeholder="请输入联系人姓名" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="联系人电话" prop="lxrDh">
            <el-input v-model="queryParams.lxrDh" placeholder="请输入联系人电话" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="城市" prop="lxrDh">
            <el-input v-model="queryParams.city" placeholder="请输入联系人电话" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="一级分类" prop="lxNameYj">
            <el-input v-model="queryParams.lxNameYj" placeholder="请输入一级分类" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="子分类" prop="lxNameEj">
            <el-input v-model="queryParams.lxNameEj" placeholder="请输入子分类" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="审核状态" prop="shzt">
            <el-select v-model="queryParams.shzt" placeholder="请选择审核状态" clearable>
              <el-option
                v-for="dict in business_information_zt"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <!--          <el-form-item label="发布人id" prop="userId">
                      <el-input v-model="queryParams.userId" placeholder="请输入发布人id" clearable style="width: 240px" @keyup.enter="handleQuery" />
                    </el-form-item>-->
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['business:information:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['business:information:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['business:information:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['business:information:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!--        <el-table-column label="" align="center" prop="id" v-if="true" />-->
        <!--        <el-table-column label="分类id" align="center" prop="flId" />-->
        <!--        <el-table-column label="信息类型" align="center" prop="type">-->
        <!--          <template #default="scope">-->
        <!--            <dict-tag :options="business_information_type" :value="scope.row.type"/>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="标题" align="center" prop="bt" />
        <el-table-column label="一级分类" align="center" prop="lxNameYj" />
        <el-table-column label="子分类" align="center" prop="lxNameEj" />
        <!--                <el-table-column label="内容" align="center" prop="nr" />
                                <el-table-column label="图片" align="center" prop="photo" width="200px">
                                  <template #default="scope">
                        &lt;!&ndash;              <ImagePreview :width="100" :height="100" :src="scope.row.photoUrl"&ndash;&gt;
                        &lt;!&ndash;                            :preview-src-list="scope.row.photoUrl?.split(',')"/>&ndash;&gt;
                                    <div>
                                    <video :src="scope.row.photoUrl?.split(',')" style="width: 200px;height: 200px;object-fit: fill"></video>
                                    </div>
                                  </template>
                                </el-table-column>-->

        <el-table-column label="图片" align="center" prop="imgList"  width="200px">
          <template #default="scope">
            <div v-if="scope.row.imgList&&scope.row.imgList.length>0">
            <ImagePreview  :width="100" :height="100" :src="scope.row.imgList[0]"
                          :preview-src-list="scope.row.imgList"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="视频" align="center" prop="urlLink"  width="200px">
          <template #default="scope">
            <el-button v-if="scope.row.videoList&&scope.row.videoList.length>0" type="primary" @click="handleShowImg(scope.row.videoList)">查看</el-button>
            <el-button v-else type="info" >查看</el-button>
          </template>
        </el-table-column>
        <!--        <el-table-column label="地址" align="center" prop="dz" />-->
        <el-table-column label="省" align="center" prop="province" />
        <el-table-column label="市" align="center" prop="city" />
        <el-table-column label="区" align="center" prop="districts" />
        <el-table-column label="内容" align="center" prop="nr" width="200px" show-overflow-tooltip />
        <!--        <el-table-column label="身份" align="center" prop="sf" />-->
        <el-table-column label="联系人姓名" align="center" prop="lxrXm" width="200px" show-overflow-tooltip />
        <el-table-column label="联系人电话" align="center" prop="lxrDh" width="200px" show-overflow-tooltip/>
        <el-table-column label="审核状态" align="center" prop="shzt">
          <template #default="scope">
            <dict-tag :options="business_information_zt" :value="scope.row.shzt"/>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" align="center" prop="createTime" width="200px" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="审核理由" align="center" prop="shly" width="200px" show-overflow-tooltip />
        <el-table-column label="过期时间" align="center" prop="expirationDate" width="200px" show-overflow-tooltip />
        <!--        <el-table-column label="发布人id" align="center" prop="userId" />-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:information:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:information:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="50%" append-to-body>
      <el-form ref="informationFormRef" :model="form" :rules="rules" label-width="100px">
        <!--        <el-form-item label="分类id" prop="flId">
                  <el-input v-model="form.flId" placeholder="请输入分类id" />
                </el-form-item>-->
        <!--        <el-form-item label="信息类型" prop="type">-->
        <!--          <el-radio-group v-model="form.type">-->
        <!--            <el-radio-->
        <!--                v-for="dict in business_information_type"-->
        <!--                :key="dict.value"-->
        <!--                :label="dict.value"-->
        <!--            >{{dict.label}}</el-radio>-->
        <!--          </el-radio-group>-->
        <!--        </el-form-item>-->
        <el-form-item label="标题" prop="bt">
          <el-input v-model="form.bt" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="一级分类" prop="lxNameYj">
          <el-input v-model="form.lxNameYj" placeholder="请输入一级分类" />
        </el-form-item>
        <el-form-item label="子分类" prop="lxNameEj">
          <el-input v-model="form.lxNameEj" placeholder="请输入子分类" />
        </el-form-item>
        <el-form-item label="地址" prop="province">
          {{form.province}}{{form.city}}{{form.districts}}
<!--          <el-input v-model="form.province" placeholder="请输入地址" />-->
        </el-form-item>

        <el-form-item label="内容" prop="nr">
          <el-input v-model="form.nr"  type="textarea" autosize  placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="lxrXm">
          <el-input v-model="form.lxrXm" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="lxrDh">
          <el-input v-model="form.lxrDh" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="发布时间" prop="createTime">
          <el-input v-model="form.createTime" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="过期时间" prop="expirationDate">
          <el-input v-model="form.expirationDate" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="审核状态" prop="shzt">
          <el-radio-group v-model="form.shzt">
            <el-radio
              v-for="dict in business_information_zt"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核理由" prop="shly">
          <el-input v-model="form.shly" placeholder="请输入审核理由" />
        </el-form-item>
        <!--        <el-form-item label="发布人id" prop="userId">
                  <el-input v-model="form.userId" placeholder="请输入发布人id" />
                </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 查看图片 -->
    <el-dialog title="图片查看" v-model="showImg" width="50%" append-to-body @close="handleImgClose">
      <div v-if="videoList.length>0">
        <video v-for="(item,index) in videoList" :src="item" :key="index" controls="controls" width="100%" ></video>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Information" lang="ts">
import { listInformation, getInformation, delInformation, addInformation, updateInformation } from '@/api/business/information';
import { InformationVO, InformationQuery, InformationForm } from '@/api/business/information/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { business_information_zt, business_information_type } = toRefs<any>(proxy?.useDict('business_information_zt', 'business_information_type'));

const informationList = ref<InformationVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const informationFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: InformationForm = {
  id: undefined,
  flId: undefined,
  type: undefined,
  bt: undefined,
  btlxNameYj: undefined,
  lxNameEj: undefined,
  nr: undefined,
  photo: undefined,
  dz: undefined,
  sf: undefined,
  lxrXm: undefined,
  lxrDh: undefined,
  city: undefined,
  shzt: undefined,
  shly: undefined,
  userId: undefined,
}
const data = reactive<PageData<InformationForm, InformationQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    flId: undefined,
    type: undefined,
    bt: undefined,
    btlxNameYj: undefined,
    lxNameEj: undefined,
    nr: undefined,
    photo: undefined,
    dz: undefined,
    sf: undefined,
    lxrXm: undefined,
    lxrDh: undefined,
    shzt: undefined,
    shly: undefined,
    userId: undefined,
    params: {
    }
  },
  rules: {
    /*id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    flId: [
      { required: true, message: "分类id不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "1供应 2求购不能为空", trigger: "change" }
    ],
    bt: [
      { required: true, message: "标题不能为空", trigger: "blur" }
    ],
    nr: [
      { required: true, message: "内容不能为空", trigger: "blur" }
    ],
    photo: [
      { required: true, message: "图片不能为空", trigger: "blur" }
    ],
    dz: [
      { required: true, message: "地址不能为空", trigger: "blur" }
    ],
    sf: [
      { required: true, message: "身份不能为空", trigger: "blur" }
    ],
    lxrXm: [
      { required: true, message: "联系人姓名不能为空", trigger: "blur" }
    ],
    lxrDh: [
      { required: true, message: "联系人电话不能为空", trigger: "blur" }
    ],
    shzt: [
      { required: true, message: "审核状态 1未审核 2待审核 3审核通过 4审核未通过不能为空", trigger: "change" }
    ],
    shly: [
      { required: true, message: "审核理由不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "发布人id不能为空", trigger: "blur" }
    ],*/
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listInformation(queryParams.value);
  if (res.rows.length>0){
    for (let i = 0; i < res.rows.length; i++) {
      if (res.rows[i].photoUrl){
        res.rows[i].videoList=[];
        res.rows[i].imgList=[];
        let imgAll = res.rows[i].photoUrl.split(',');
        for (let j = 0; j < imgAll.length; j++) {
          if (imgAll[j].indexOf('mp4') > 0) {
            res.rows[i].videoList.push(imgAll[j])
          }else{
            res.rows[i].imgList.push(imgAll[j])
          }
        }
      }
    }
  }
  informationList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  informationFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: InformationVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 查看图片 */
let imgList = ref([])
let videoList = ref([])
let showImg = ref(false)
const handleShowImg = (video) => {
  if (video.length==0){
    proxy?.$modal.msgError("暂无视频");
  }else{
    videoList.value=video
    showImg.value = true
  }
}
const handleImgClose = () => {
  imgList.value=[]
  videoList.value=[]
  showImg.value = false
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: InformationVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getInformation(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改信息";
}

/** 提交按钮 */
const submitForm = () => {
  informationFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateInformation(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addInformation(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: InformationVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除信息编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delInformation(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('business/information/export', {
    ...queryParams.value
  }, `information_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
